package com.hb.gp.impl;

import java.util.List;
import java.util.Map;

import com.hb.gp.GPInsCommand;
import com.hb.gp.GPType;
import com.hb.gp.IGPCommandMessageService;
import com.hb.gp.util.tools.GP_Tools;

/**
 * 
 * @author shangzk
 *
 */
@GPType(INS = GPInsCommand.INS_E6)
public class GPInstall implements IGPCommandMessageService {

	private String installForExtradition(String sdAid, String appAid,
			String extraditionParamsField) {
		String token = "";// token的生成需要外部环境的支持
		// 由后向前拼装指令
		String request = GP_Tools.getHexLength(token) + token;
		request = GP_Tools.getHexLength(extraditionParamsField)
				+ extraditionParamsField + request;
		request = "00" + request;
		request = GP_Tools.getHexLength(appAid) + appAid + request;
		request = "00" + request;
		request = GP_Tools.getHexLength(sdAid) + sdAid + request;
		request = "84E61000" + GP_Tools.getHexLength(request) + request;
		return request;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String makeApduMap(String apdu, Map<String, Object> map) {
		// TODO Auto-generated method stub
		List<String> sessionKey = (List<String>) map
				.get(IGPCommandMessageService.KEY_SESSION);
		String lastMac = (String) map
				.get(IGPCommandMessageService.VARIBLE_LASTMAC);
		apdu = GP_Tools.getApduMac(apdu, sessionKey.get(1), lastMac);
		// 写入mac
		map.put(IGPCommandMessageService.VARIBLE_LASTMAC,
				apdu.substring(apdu.length() - 16));
		return apdu;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String makeApduMap(String apdu, Map<String, Object> varibleMap,
			Map<String, Object> storeMap) {
		// TODO Auto-generated method stub
		List<String> sessionKey = (List<String>) storeMap.get(KEY_SESSION);
		String lastMac = (String) storeMap.get(VARIBLE_LASTMAC);
		apdu = apdu.trim().replaceAll(" ", "");
		int begin = apdu.indexOf("(") + 1;
		int end = apdu.indexOf(")");
		String[] params = apdu.substring(begin, end).split(",");
		switch (apdu.substring(4, 8)) {
		case "0C00":
			apdu = installForInstall(params, varibleMap);
			break;
		case "0200":
			apdu = installForLoad(params, varibleMap);
			break;
		case "1000":// install for Extradition
			apdu = installForExtradition("A0000006320100000000000000000080",
					"A0000006320100000000008065000080", "");
			break;
		default:
			break;
		}
		// 计算mac
		apdu = GP_Tools.getApduMac(apdu, sessionKey.get(1), lastMac);
		// set mac
		storeMap.put(VARIBLE_LASTMAC, apdu.substring(apdu.length() - 16));
		return apdu;
	}

	private String installForLoad(String[] params,
			Map<String, Object> varibleMap) {
		String token = "";// token的生成需要外部环境的支持
		// 由后向前拼装指令
		String request = GP_Tools.getHexLength(token) + token;
		String appParametersFiled = (String) varibleMap.get(params[3]);
		String blockHash = (String) varibleMap.get(params[2]);
		String appAid = (String) varibleMap.get(params[1]);
		String execloadFileAid = (String) varibleMap.get(params[0]);
		request = GP_Tools.getHexLength(appParametersFiled)
				+ appParametersFiled + request;
		request = GP_Tools.getHexLength(blockHash) + blockHash + request;
		request = GP_Tools.getHexLength(appAid) + appAid + request;
		request = GP_Tools.getHexLength(execloadFileAid) + execloadFileAid
				+ request;
		request = "84E60200" + GP_Tools.getHexLength(request) + request;
		return request;
	}

	private String installForInstall(String[] params,
			Map<String, Object> varibleMap) {
		String token = "";// token的生成需要外部环境的支持
		// 由后向前拼装指令
		String request = GP_Tools.getHexLength(token) + token;
		String appParametersFiled = (String) varibleMap.get(params[4]);
		String appPrivileges = (String) varibleMap.get(params[3]);
		String appAid = (String) varibleMap.get(params[2]);
		String execmoduleAid = (String) varibleMap.get(params[1]);
		String execloadFileAid = (String) varibleMap.get(params[0]);
		request = GP_Tools.getHexLength(appParametersFiled)
				+ appParametersFiled + request;
		request = GP_Tools.getHexLength(appPrivileges) + appPrivileges
				+ request;
		request = GP_Tools.getHexLength(appAid) + appAid + request;
		request = GP_Tools.getHexLength(execmoduleAid) + execmoduleAid
				+ request;
		request = GP_Tools.getHexLength(execloadFileAid) + execloadFileAid
				+ request;
		request = "84E60C00" + GP_Tools.getHexLength(request) + request;
		return request;
	}

}
